import store from '../store/index'
import axios from '@/service/HttpRequest'
import serviceApi from '@/service/api'
import { ElMessage } from 'element-plus'
// 权限校验
export function check_role(role) {
    // 企业管理员 默认拥有该等级企业下 所有权限  
    if (store.state.user_info.isAdmin) { // 超管
        return true
    }
    var list = store.state.check_role_list
    return list.some(item => {
        return item.path == role.path
    })
}
// 时间格式转换
export function formatDate(date, cut) {
    var date_1 = new Date(date);
    var YY = date_1.getFullYear() + '-';
    var MM =
        (date_1.getMonth() + 1 < 10
            ? "0" + (date_1.getMonth() + 1)
            : date_1.getMonth() + 1) + '-';
    var DD = date_1.getDate() < 10 ? "0" + date_1.getDate() : date_1.getDate();
    var hh =
        (date_1.getHours() < 10 ? "0" + date_1.getHours() : date_1.getHours()) + ":";
    var mm =
        (date_1.getMinutes() < 10 ? "0" + date_1.getMinutes() : date_1.getMinutes()) +
        ":";
    var ss = date_1.getSeconds() < 10 ? "0" + date_1.getSeconds() : date_1.getSeconds();
    return cut.indexOf('hh') != - 1 ? YY + MM + DD + " " + hh + mm + ss : YY + MM + DD;
}

// 天数差
export function days(time1, time2) {
    return parseInt((parseInt(time1 as any) - (parseInt(time2 as any))) / (24 * 60 * 60 * 1000) as any)
}
// 防抖
export function debounce(delay, fun) {
    var timer
    return function () {
        //如果定时器存在则清空定时器
        if (timer) {
            clearTimeout(timer);
        }
        //设置定时器，规定时间后执行真实要执行的函数
        timer = setTimeout(() => {
            fun()
        }, delay);
    }
}

// 获取字典值
export function dictData(id) {
    return new Promise(function (resolve) {
        axios
            .request({
                url: serviceApi.dict_list,
                method: 'post',
                data: { current: 1, pageSize: 100, dictTypeId: id },
            })
            .then((res) => {
                resolve(res.data)
            })
            .catch((error) => {
                ElMessage.error(error)
            })
    })
}
// 搜索参数处理 为后续扩展
export function handle_search_key(e) {
    //  相等  EQUAL // 大于 GREATER_THAN // 大于等于  GREATER_THAN_EQ // 小于  LESS_THAN // 小于等于  LESS_THAN_EQ // 中模糊查询  INNER_LIKE
    //  左模糊查询   LEFT_LIKE // 右模糊查询  RIGHT_LIKE // 包含  IN // 不包含  NOT_IN // 不等于 NOT_EQUAL // between  BETWEEN // 不为空  NOT_NULL // 为空 IS_NULL
    let keys = {}
    let keys_list = Object.keys(e)
    console.log(e)
    let exclude_item_list = ['timeoutStatus', 'productNum', 'profileNum']
    for (let item in e) {
        if (['current', 'pageSize'].includes(item)) {
            // 不需要报表查询的字段
            keys[item] = e[item]
        } else {
            if ((item == 'enterpriseName' && keys_list.includes('memberName')) || (item == 'enterpriseId' && keys_list.includes('memberId'))
                || (item == 'memberName' && keys_list.includes('enterpriseName')) || (item == 'memberId' && keys_list.includes('enterpriseId'))) {
                // 企业、个人为同一个查询条件
                keys[item] = {
                    type: 'OR',
                    detailList: [{ value: trim(e[item]) }]
                }
            } else if (!exclude_item_list.includes(item) &&
                (item.includes('Time') || item.includes('time') || (item.includes('date') && !item.includes('update')) || item.includes('Date'))) {
                // 时间相关
                keys[item] = {
                    type: 'BETWEEN',
                    detailList: [{ type: 'GREATER_THAN_EQ', value: e[item] ? e[item][0] : '' }, { type: 'LESS_THAN_EQ', value: !e[item] ? '' : e[item][1] == e[item][0] ? '' : e[item][1] }]
                }
            } else if (!exclude_item_list.includes(item) &&
                (item.includes('Amount') || item.includes('amount') || item.includes('Num') || item.includes('num') || item.includes('Price')
                    || item.includes('price') || item == 'allPayTotalFee' || item.includes('area') || item.includes('Commission'))) {
                // 金额相关
                keys[item] = {
                    type: 'BETWEEN',
                    detailList: [{ type: 'GREATER_THAN_EQ', value: trim(e[item][0]) }, { type: 'LESS_THAN_EQ', value: trim(e[item][1]) }]
                }
            } else {
                keys[item] = {
                    type: item.includes('Status') || item.includes('status') || item.includes('Type') || item.includes('type') ? 'EQUAL' : 'INNER_LIKE',
                    detailList: [{ value: typeof e[item] == 'boolean' ? e[item] : trim(e[item]) }]
                }
            }
        }
    }
    return keys
}
// 去除左右空格
function trim(s) {
    console.log(s)
    return typeof s == 'string' ? s.replace(/(^\s*)|(\s*$)/g, "") : s;
}